/**
  ******************************************************************************
  * File Name          : ctrl_shl.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "bsp.h"
#include "spiflash.h"
#include "collector.h"
#include "ctrl_common.h"
#include "route.h"

#if(ROUTE_VERSION == 60)

#define CTR_FAN_CHANNEL_MAXNUM       9u
#define CTR_SHL_CHANNEL_MAXNUM       1u
#define CTR_NUQ_CHANNEL_MAXNUM       1u
#define CTR_DK_CHANNEL_MAXNUM        1u

uint8_t CtrFan_GetActuator(uint8_t ch)
{
  if(ch >= CTR_FAN_CHANNEL_MAXNUM)
  {
    return 0;
  }
  
	switch(ch)
	{
		case 0:
			if(Relay01_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
      
		case 1:
			if(Relay02_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}

		case 2:
			if(Relay03_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		
		case 3:
			if(Relay04_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}

		case 4:
			if(Relay05_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}

		case 5:
			if(Relay06_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		
		case 6:
			if(Relay07_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
      
    case 7:
			if(Relay08_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
    
    case 8:
			if(Relay09_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
      
		default:
			return 0;
	}
}

uint8_t CtrFan_GetActNum(void)
{
	uint8_t num;
  
	num = 0;
	
	if(Relay01_Read() != 0)
	{
		num++;
	}
	
	if(Relay02_Read() != 0)
	{
		num++;
	}		

	if(Relay03_Read() != 0)
	{
		num++;
	}
	
	if(Relay04_Read() != 0)
	{
		num++;
	}
	
	if(Relay05_Read() != 0)
	{
		num++;
	}
	
	if(Relay06_Read() != 0)
	{
		num++;
	}
	
	if(Relay07_Read() != 0)
	{
		num++;
	}
	
	if(Relay08_Read() != 0)
	{
		num++;
	}

	if(Relay09_Read() != 0)
	{
		num++;
	}
			
	return num;
}

void CtrFan_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu)
{
  if(ch >= CTR_FAN_CHANNEL_MAXNUM)
  {
    return;
  }
  
	if((mu != 0)&&(Ctrl_ParaMD.mode != CTRL_MODE_MU))
	{
		return;
	}
	
	if((mu != 0)&&(Ctrl_ParaMD.mode == CTRL_MODE_MU))
	{
		if(act == 1)
		{
			Ctrl_StateFN.fnst[ch] = 'M';
		}
		else if(act == 0)
		{
			Ctrl_StateFN.fnst[ch] = 'm';
		}
	}
  
  if(Ctrl_ParaTD.fntd[ch] == 0)
  {
    act = 0;
    Ctrl_StateFN.fnst[ch] = 'n';
  }
  
	switch(ch)
	{
		case 0:
			if(act == 1)
			{
				Relay01(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay01(RELAY_OFF);
			}
			break;
			
		case 1:
			if(act == 1)
			{
				Relay02(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay02(RELAY_OFF);
			}
			break;
		
		case 2:
			if(act == 1)
			{
				Relay03(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay03(RELAY_OFF);
			}
			break;
		
		case 3:
			if(act == 1)
			{
				Relay04(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay04(RELAY_OFF);
			}
			break;
		
		case 4:
			if(act == 1)
			{
				Relay05(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay05(RELAY_OFF);
			}
			break;
		
		case 5:
			if(act == 1)
			{
				Relay06(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay06(RELAY_OFF);
			}
			break;
		
		case 6:
			if(act == 1)
			{
				Relay07(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay07(RELAY_OFF);
			}
			break;
    
    case 7:
			if(act == 1)
			{
				Relay08(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay08(RELAY_OFF);
			}
			break;
    
    case 8:
			if(act == 1)
			{
				Relay09(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay09(RELAY_OFF);
			}
			break;
      
		default:
			return;
	}
  
  if(act == 1)
  {
    vTaskDelay(86);
    return;
  }
  
  return;
}

uint8_t CtrShl_GetActuator(uint8_t ch)
{
  if(ch >= CTR_SHL_CHANNEL_MAXNUM)
  {
    return 0;
  }
  
	switch(ch)
	{
		case 0:
			if(Relay10_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}

		default:
			return 0;
	}
}

void CtrShl_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu)
{
  if(ch >= CTR_SHL_CHANNEL_MAXNUM)
  {
    return;
  }
  
	if((mu != 0)&&(Ctrl_ParaMD.mode != CTRL_MODE_MU))
	{
		return;
	}
	
	if((mu != 0)&&(Ctrl_ParaMD.mode == CTRL_MODE_MU))
	{
		if(act == 1)
		{
			Ctrl_StateSL.slst[ch] = 'M';
		}
		else if(act == 0)
		{
			Ctrl_StateSL.slst[ch] = 'm';
		}
	}
	
  if(Ctrl_ParaTD.sltd[ch] == 0)
  {
    act = 0;
    Ctrl_StateSL.slst[ch] = 'n';
  }
  
	switch(ch)
	{
		case 0:
			if(act == 1)
			{
				Relay10(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay10(RELAY_OFF);
			}
			break;
      
		default:
			return;
	}
  
  if(act == 1)
  {
    vTaskDelay(86);
    return;
  }
  
  return;
}

uint8_t CtrNuq_GetActuator(uint8_t ch)
{
  if(ch >= CTR_NUQ_CHANNEL_MAXNUM)
  {
    return 0;
  }
  
	switch(ch)
	{
		case 0:
			if(Relay12_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
      
		default:
			return 0;
	}
}

void CtrNuq_SetActuator(volatile uint8_t act, uint8_t ch, uint8_t mu)
{
  if(ch >= CTR_NUQ_CHANNEL_MAXNUM)
  {
    return;
  }
  
	if((mu != 0)&&(Ctrl_ParaMD.mode != CTRL_MODE_MU))
	{
		return;
	}
	
	if((mu != 0)&&(Ctrl_ParaMD.mode == CTRL_MODE_MU))
	{
		if(act == 1)
		{
			Ctrl_StateNQ.nqst[ch] = 'M';
		}
		else if(act == 0)
		{
			Ctrl_StateNQ.nqst[ch] = 'm';
		}
	}
  
	if(Ctrl_ParaTD.nqtd[ch] == 0)
  {
    act = 0;
    Ctrl_StateNQ.nqst[ch] = 'n';
  }
  
	switch(ch)
	{
		case 0:
			if(act == 1)
			{
				Relay12(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay12(RELAY_OFF);
			}
			break;
      
		default:
			return;
	}
  
  if(act == 1)
  {
    vTaskDelay(86);
    return;
  }

  return;
}

void CtrBj_SetActuator(volatile uint8_t act)
{
  return;
}

void CtrZd_SetActuator(volatile uint8_t act)
{
  return;
}

uint8_t CtrDk_GetActuator(uint8_t ch)
{
  if(ch >= CTR_DK_CHANNEL_MAXNUM)
  {
    return 0;
  }
  
  switch(ch)
	{
		case 0:
			if(Relay11_Read() != 0)
			{
				return 1;
			}
			else
			{
				return 0;
			}
      
		default:
			return 0;
	}
}

void CtrDk_SetActuator(volatile uint8_t act, uint8_t ch)
{
  if(ch >= CTR_DK_CHANNEL_MAXNUM)
  {
    return;
  }
  
  switch(ch)
	{
		case 0:
			if(act == 1)
			{
				Relay11(RELAY_ON);
			}
			else if(act == 0)
			{
				Relay11(RELAY_OFF);
			}
			break;
      
		default:
			return;
	}
  
  Ctrl_StateDK.ptkg[ch] = CtrDk_GetActuator(ch);
}

static Route_PayloadType xc_action;

void CtrXc_SetActuator(volatile uint8_t act)
{
  uint8_t i;
  
  /* 此处是开度 */
  if(act > 100)
  {
    return;
  }
  
  xc_action.dev_type  = RT_DV_XC;
  xc_action.pl_len    = sizeof(Xc_SetParaType);
  xc_action.pl_type   = ROUTE_PLTYP_XC_SETPARA;
  xc_action.trans_dir = ROUTE_TRSDIR_LOCAL_TO_SUBDEV;
  ((Xc_SetParaType *)(xc_action.payload))->curt_pos = 100-act; /* 切换关度 */
  ((Xc_SetParaType *)(xc_action.payload))->out_time = 253; /* 一个无效值,目的是不破坏小窗参数 */ 
  
  for(i=0; i<ROUTE_DEVICE_MAXNUM; i++)
  {
    if((Route_DeviceList.xc[i].did[0] == 'X') && (Route_DeviceList.xc[i].did[1] == 'C'))
    {
      xc_action.dev_addr  = idx2add(i);
      if(Route_EvewTo485_Qhd != NULL)
      {
        xQueueSend(Route_EvewTo485_Qhd, &xc_action, 0);
      }
    }
  }

  return;
}

#endif
